package com.example.lenovo.test1;


import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;

import androidx.appcompat.app.AppCompatActivity;

public class Main11Activity extends AppCompatActivity {

    private ArrayList<ListViewItemData> mListViewData;
    private ListView mListview4Consume;

    private  ListViewItemData createListViewItemData(int order,String date, String money){
        ListViewItemData listViewItemData = new ListViewItemData();
        listViewItemData.order = order;
        listViewItemData.date = date;
        listViewItemData.money = money;
        return  listViewItemData;
    }
    protected void initData() {
        mListViewData = new ArrayList<ListViewItemData>();
        mListViewData.add(createListViewItemData(1,"1-01","10"));
        mListViewData.add(createListViewItemData(2,"1-10","13"));
        mListViewData.add(createListViewItemData(3,"1-13","15"));
        mListViewData.add(createListViewItemData(4,"1-16","16"));
    }
    private void initListHeader() {
        ViewGroup headerView = (ViewGroup) LayoutInflater.from(this).inflate(
                R.layout.listview_activity_list_item,
                mListview4Consume,
                false);
        TextView tvId = (TextView) headerView.getChildAt(0);
        TextView tvBusId = (TextView) headerView.getChildAt(1);
        TextView tvCapacity = (TextView) headerView.getChildAt(2);
        tvId.setText("序号");
        tvBusId.setText("消费 日期");
        tvCapacity.setText("消费金额");
        mListview4Consume.addHeaderView(headerView);
    }
    private void initListFooter() {
        ViewGroup footerView = (ViewGroup) LayoutInflater.from(this).inflate(
                R.layout.listview_activity_foot_item,
                mListview4Consume,
                false);
        mListview4Consume.addFooterView(footerView);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main11);
        mListview4Consume = findViewById(R.id.myListView);
        initData();
        initListHeader();
        initListFooter();
        ListviewActivityListItemAdapter adapter = new ListviewActivityListItemAdapter(Main11Activity.this);
        mListview4Consume.setAdapter(adapter);
    }
    private class  ListViewItemData{
        int order;
        String date;
        String money;
    }

    public class ListviewActivityListItemAdapter extends BaseAdapter {

        private Context context;
        private LayoutInflater layoutInflater;
        public ListviewActivityListItemAdapter(Context context) {
            this.context = context;
            this.layoutInflater = LayoutInflater.from(context);
        }

        @Override
        public int getCount() {
            return mListViewData.size();
        }

        @Override
        public Object getItem(int position) {
            return mListViewData.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = layoutInflater.inflate(R.layout.listview_activity_list_item, null);
                convertView.setTag(new ViewHolder(convertView));
            }
            initializeViews((ListViewItemData)getItem(position), (ViewHolder) convertView.getTag());
            return convertView;
        }

        private void initializeViews(ListViewItemData object, ViewHolder holder) {
            holder.tvOrder.setText(object.order+"");
            holder.tvDate.setText(object.date);
            holder.tvMoney.setText(object.money);
        }

        protected class ViewHolder {
            private TextView tvOrder;
            private TextView tvDate;
            private TextView tvMoney;

            public ViewHolder(View view) {
                tvOrder = (TextView) view.findViewById(R.id.tv_order);
                tvDate = (TextView) view.findViewById(R.id.tv_date);
                tvMoney = (TextView) view.findViewById(R.id.tv_money);
            }
        }
    }


}
